//
//  CZWellcomViewController.swift
//  swiftweibo
//
//  Created by Apple on 15/11/26.
//  Copyright © 2015年 tzw. All rights reserved.
//

import UIKit
import SDWebImage
class CZWellcomViewController: UIViewController {
    // 定义一个iconView的属性
    private var iconViewButtonCon: NSLayoutConstraint?
    override func viewDidLoad() {
        super.viewDidLoad()

        // 准备UI
        prepareUI()
        // 获取用户数据
        CZUserAccount.loadUserAccount()?.loadUserInfo()
        // 加载用户头像
        if let avatar_large = CZUserAccount.loadUserAccount()?.avatar_large{
//            print("avatar_large:\(avatar_large)")
         iconView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        

    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        // 设置动画
        moveAnimatetion()
    }
    // 设置图像的动画
    private func moveAnimatetion(){
        // 使用自动布局之后只要修改约束就可以了
        iconViewButtonCon?.constant = -500
        // 动画
        UIView.animateWithDuration(1.2, delay: 0.1, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 更新布局
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
//                print("完成动画")
                // 切换控制器
                AppDelegate.switchRootViewController(true)
        }
    }
    // 准备UI
    func prepareUI(){
        // 添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        // 添加约束
//        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 设置背景图片的左和右边
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bkg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkg" : bkgImageView]))
//        // 上下
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bkg]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkg" : bkgImageView]))
        bkgImageView.ff_Fill(view)
        // 设置头像x
       let con = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 85, height: 85), offset: CGPoint(x: 0, y: -160))
        iconViewButtonCon = iconView.ff_Constraint(con, attribute: NSLayoutAttribute.Bottom)
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        iconViewButtonCon = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
//        view.addConstraint(iconViewButtonCon!)
//        // 头像的宽度
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        // 设置label
        messageLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil)
//        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
 
    }
    // MARK: - 懒加载控件
    // 背景
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    // 头像
    private lazy var iconView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "avatar_default_big"))
        view.layer.cornerRadius = 42.5
        view.layer.masksToBounds = true
        return view
        }()
    private lazy var messageLabel: UILabel = {
            let label = UILabel()
            label.text = "欢迎归来"
            label.sizeToFit()
            return label
        }()
}
